package com.afd.ozblip.util;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.afd.ozblip.entity.AddressEntity;
import com.afd.ozblip.net.HttpUtil;

public class LocationUtil {

	private final static double EARTH_RADIUS = 6371.004;

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	/**
	 * 获取两个经纬度的距离
	 * 
	 * @param lat1
	 *            纬度1
	 * @param lng1
	 *            经度1
	 * @param lat2
	 *            纬度2
	 * @param lng2
	 *            经度2
	 * @return
	 */
	public static double GetDistance(double lat1, double lon1, double lat2,
			double lon2) {
		// double radLat1 = rad(lat1);
		// double radLat2 = rad(lat2);
		// double a = radLat1 - radLat2;
		// double b = rad(lng1) - rad(lng2);
		// double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
		// + Math.cos(radLat1) * Math.cos(radLat2)
		// * Math.pow(Math.sin(b / 2), 2)));
		// s = s * EARTH_RADIUS;
		// s = Math.round(s * 10000) / 10000;
		// return s;
		lat1 = Math.toRadians(lat1);
		lon1 = Math.toRadians(lon1);
		lat2 = Math.toRadians(lat2);
		lon2 = Math.toRadians(lon2);
		double d = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1)
				* Math.cos(lat2) * Math.cos(lon1 - lon2);
		return (EARTH_RADIUS * Math.acos(d));
	}

	/**
	 * 计算方位角,正东向为0度，以顺时针方向递增
	 * 
	 * @param lat1
	 *            纬度1
	 * @param lng1
	 *            经度1
	 * @param lat2
	 *            纬度2
	 * @param lng2
	 *            经度2
	 * @return
	 */
	public static double GetAzimuth(double lat1, double lon1, double lat2,
			double lon2) {
		double result = 0.0;

		int ilat1 = (int) (0.50 + lat1 * 360000.0);
		int ilat2 = (int) (0.50 + lat2 * 360000.0);
		int ilon1 = (int) (0.50 + lon1 * 360000.0);
		int ilon2 = (int) (0.50 + lon2 * 360000.0);

		lat1 = Math.toRadians(lat1);
		lon1 = Math.toRadians(lon1);
		lat2 = Math.toRadians(lat2);
		lon2 = Math.toRadians(lon2);

		if ((ilat1 == ilat2) && (ilon1 == ilon2)) {
			return result;
		} else if (ilon1 == ilon2) {
			if (ilat1 > ilat2)
				result = 180.0;
		} else {
			double c = Math
					.acos(Math.sin(lat2) * Math.sin(lat1) + Math.cos(lat2)
							* Math.cos(lat1) * Math.cos((lon2 - lon1)));
			double A = Math.asin(Math.cos(lat2) * Math.sin((lon2 - lon1))
					/ Math.sin(c));
			result = Math.toDegrees(A);
			if ((ilat2 > ilat1) && (ilon2 > ilon1)) {
			} else if ((ilat2 < ilat1) && (ilon2 < ilon1)) {
				result = 180.0 - result;
			} else if ((ilat2 < ilat1) && (ilon2 > ilon1)) {
				result = 180.0 - result;
			} else if ((ilat2 > ilat1) && (ilon2 < ilon1)) {
				result += 360.0;
			}
		}
		return result;
	}

	// private String QUERYADDRESS =
	// "http://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=true&language=zh_cn";

	public static AddressEntity queryLocation(Context paramContext, Double lat,
			Double lng) {
		String QUERYADDRESS = "http://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=true&language=zh_cn";
		String url = String.format(QUERYADDRESS, lat, lng);
		byte[] resultByte = HttpUtil.requestByGet(paramContext, url);
		AddressEntity address = null;
		try {
			JSONArray jsonArray = new JSONObject(
					new String(resultByte, "UTF-8")).getJSONArray("results");
			if (jsonArray.length() > 1) {
				JSONObject jsonObject = jsonArray.getJSONObject(0);
				address = new AddressEntity();
				// jsonObject.getString("formatted_address");
				JSONArray addressArray = jsonObject
						.getJSONArray("address_components");
				String addressJsonStr = addressArray.toString();
				String addressStr = "";
				if (addressJsonStr.contains("locality")) {
					for (int i = addressArray.length() - 1; i > -1; i--) {
						JSONObject jsonObj = addressArray.getJSONObject(i);
						String tempStr = jsonObj.getString("long_name");
						String typesStr = jsonObj.getString("types");
						if (typesStr.contains("postal_code")||typesStr.contains("country")) {
							
						}else{
							addressStr += tempStr;
						}
						if (typesStr.contains("\"locality\",")) {
							address.setNowCity(tempStr);
						}
					}
				}
				address.setStreetNum("");
				address.setStreet("");
				address.setDistrict("");
				address.setCity("");
				address.setProvince(addressStr);

				// address.setProvince(addressArray.getJSONObject(4).getString("long_name"));

			}
			return address;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return address;

	}
}
